package com.example.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.exam.mapper.TUserMapper;
import com.example.exam.pojo.entity.TUser;
import com.example.exam.pojo.vo.UserVO.KeyValue;
import com.example.exam.pojo.vo.UserVO.UserPageVO;
import com.example.exam.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//import com.sun.org.apache.regexp.internal.RE;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * @author LENOVO
 * @description TODO
 * @date 2023/2/3 17:39
 */
@Service
public class UserServiceImpl  implements IUserService {

    @Autowired
    private TUserMapper userMapper;
    @Override
    public PageInfo<TUser> userPage(UserPageVO userPageVO) {
       /* return PageHelper.startPage(userPageVO.getPageIndex(),userPageVO.getPageSize(),"id desc")
                .doSelectPage(()->
                        userMapper.userPage(userPageVO));
    }*/

        PageInfo<TUser> res = PageHelper.startPage(userPageVO.getPageIndex(), userPageVO.getPageSize(), "id desc")
                .doSelectPageInfo(() -> userMapper.userPage(userPageVO));
        return  res;
    }

    @Override
    public TUser getUserById(Integer id) {
        TUser tUser = userMapper.selectById(id);
        return tUser;
    }

    @Override
    public TUser getUserByUserName(String name) {
    return    userMapper.selectByName(name);
    }

    @Override
    public int insertByFilter(TUser tUser) {
        int result = userMapper.insert(tUser);

        return result;
    }

    @Override
    public int updateByIdFilter(TUser tUser) {
        return  userMapper.updateById(tUser);
    }

    @Override
    public List<KeyValue> selectByUserName(String name) {
        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted",0).like("user_name",name).last("limit 5");

        List<TUser> tUsers = userMapper.selectList(wrapper);
        List<KeyValue> keyValues = new ArrayList<KeyValue>();
        for(TUser tUser:tUsers){
            KeyValue keyValue = new KeyValue();
            keyValue.setName(tUser.getUserName());
            keyValue.setValue(tUser.getId());
            keyValues.add(keyValue);
        }
     return keyValues;
    }
}
